package com.ydj.framework.utils.generator;

import cn.hutool.core.io.FileUtil;
import com.ydj.framework.exception.CommonException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

/**
 * 数据库实体类生成
 */
class InterfaceGenerator {

    private final static String CLASS_NAME_LOWER_CASE_FLAG = "${ClassNameLowerCase}";
    private final static String CLASS_NAME_UPPER_CASE_FLAG = "${ClassNameUpperCase}";
    private final static String CHINESE_CLASS_NAME_FLAG = "${ChineseClassName}";
    private final static String MODEL_NAME_LOWER_CASE_FLAG = "${ModelNameLowerCase}";
    private final static String MODEL_NAME_UPPER_CASE_FLAG = "${ModelNameUpperCase}";


    public static void generator(File projectDir, File templateDir, String modelName, List<String> classNameArray, List<String> chineseClassNameArray) throws IOException, CommonException {

        // 找到输出的目录
        File protocolOutputBaseDir = new File(projectDir, "app/src/main/java/com/ydj/app/protocol/http");
        System.out.println("协议输出路径是" + protocolOutputBaseDir.getAbsolutePath());

        File tempOutputBaseDir = new File(projectDir, "app/src/main/java/com/ydj/app/temp");
        System.out.println("Controller Service 输出路径是" + tempOutputBaseDir.getAbsolutePath());

        String upperModelName = GeneratorUtils.uppercaseFirstLetter(modelName);
        String lowerModelName = GeneratorUtils.lowercaseFirstLetter(modelName);

        // 生成代码了
        List<GeneratorOutputInfo> generatorOutputInfoList = new LinkedList<>();
        String javaCode;
        for (int i = 0; i < classNameArray.size(); i++) {
            String className = classNameArray.get(i);
            String chineseClassName = chineseClassNameArray.get(i);
            // controller
            javaCode = FileUtil.readString(new File(templateDir, "Controller.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(tempOutputBaseDir, null, upperModelName + className + "Controller", javaCode));

            // service
            javaCode = FileUtil.readString(new File(templateDir, "Service.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(tempOutputBaseDir, null, className + "Service", javaCode));

            // CreateReq
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolCreateReq.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Create" + className + "Req", javaCode));
            // CreateRes
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolCreateRes.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Create" + className + "Res", javaCode));
            // UpdateReq
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolUpdateReq.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Update" + className + "Req", javaCode));
            // DeleteReq
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolDeleteReq.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Delete" + className + "Req", javaCode));
            // GetListReq
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolGetListReq.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Get" + className + "ListReq", javaCode));
            // GetListRes
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolGetListRes.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Get" + className + "ListRes", javaCode));
            // GetDetailReq
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolGetDetailReq.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Get" + className + "DetailReq", javaCode));
            // GetDetailRes
            javaCode = FileUtil.readString(new File(templateDir, "ProtocolGetDetailRes.template"), StandardCharsets.UTF_8);
            javaCode = replace(javaCode, modelName, chineseClassName, className);
            generatorOutputInfoList.add(new GeneratorOutputInfo(protocolOutputBaseDir, modelName, upperModelName + className + "Get" + className + "DetailRes", javaCode));
        }

        // 检查输出
        for (GeneratorOutputInfo info : generatorOutputInfoList) {
            if (info.getFile().exists()) {
                //CommonException.throwException("文件" + info.getFile().getAbsolutePath() + "已经存在, 终止输出");
            }
        }

        // 真正生成文件
        for (GeneratorOutputInfo info : generatorOutputInfoList) {
            FileUtil.writeString(info.getJavaCode(), info.getFile(), StandardCharsets.UTF_8);
            GeneratorUtils.log("输出文件成功。路径为" + info.getFile().getAbsolutePath());
        }
        GeneratorUtils.log("接口文件输出完毕, Good Luck!");
    }

    private static String replace(String javaCode, String modelName, String chineseClassName, String className) {
        String temp = javaCode;
        temp = temp.replace(CLASS_NAME_LOWER_CASE_FLAG, GeneratorUtils.lowercaseFirstLetter(className));
        temp = temp.replace(CLASS_NAME_UPPER_CASE_FLAG, GeneratorUtils.uppercaseFirstLetter(className));
        temp = temp.replace(MODEL_NAME_LOWER_CASE_FLAG, GeneratorUtils.lowercaseFirstLetter(modelName));
        temp = temp.replace(MODEL_NAME_UPPER_CASE_FLAG, GeneratorUtils.uppercaseFirstLetter(modelName));
        temp = temp.replace(CHINESE_CLASS_NAME_FLAG, GeneratorUtils.lowercaseFirstLetter(chineseClassName));

        return temp;
    }
}